package view.items;

import java.awt.Dimension;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.enumTypes.CSMoveStyle;

import constants.Icons;
import controller.CompleteListener;






public class MovePanel extends ItemPanel implements FocusListener {

	private static final String[] moveStrings = {"Run", "Walk", "Crouch"};
	
	private JButton insButton;
	private JComboBox movesBox;
	private JTextField waitField;
	
	public MovePanel() {
		super();
		
		insButton = new JButton();
		movesBox = new JComboBox(moveStrings);
		waitField = new JTextField("0",3);
		
		JPanel row1 = new JPanel();
		JPanel row2 = new JPanel();
		
		addButton.setActionCommand("addMove");
		delButton.setActionCommand("delMove");
		insButton.setActionCommand("insMove");
		
		addButton.setToolTipText("Adds a move node");
		delButton.setToolTipText("Removes the selected move node");
		insButton.setToolTipText("Inserts a move node\n"+
				"after the selected move node");
		
		setAddIcon(Icons.getIcon("addMove"));
		setDelIcon(Icons.getIcon("delMove"));
		setInsIcon(Icons.getIcon("insMove"));
		
		this.setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
		row1.setLayout(new BoxLayout(row1,BoxLayout.LINE_AXIS));
		row2.setLayout(new BoxLayout(row2,BoxLayout.LINE_AXIS));
		
		this.setBorder(BorderFactory.createTitledBorder("Move"));
		row1.setBorder(BorderFactory.createEmptyBorder(0, 2, 3, 2));
		row2.setBorder(BorderFactory.createEmptyBorder(3, 2, 0, 2));
		
		row1.add(addButton);
		row1.add(Box.createHorizontalStrut(5));
		row1.add(insButton);
		row1.add(Box.createHorizontalStrut(5));
		row1.add(delButton);
		
		row2.add(movesBox);
		row2.add(Box.createHorizontalStrut(5));
		row2.add(new JLabel("and wait for"));
		row2.add(Box.createHorizontalStrut(5));
		row2.add(waitField);
		row2.add(Box.createHorizontalStrut(5));
		row2.add(new JLabel("sec."));
		
		this.add(row1);
		this.add(row2);
		this.add(Box.createVerticalGlue());
		
		waitField.addFocusListener(this);
		
		insButton.setMaximumSize(new Dimension(20,20));
		insButton.setMinimumSize(new Dimension(20,20));
		
		movesBox.setMaximumSize(new Dimension(60,20));
		waitField.setMaximumSize(new Dimension(60,20));
		waitField.setMinimumSize(new Dimension(20,20));
		this.setMaximumSize(new Dimension(240,80));
	}
	
	public CSMoveStyle getMoveStyle() {
		switch (movesBox.getSelectedIndex()) {
		case 0:
			return CSMoveStyle.RUN;
		case 1:
			return CSMoveStyle.WALK;
		case 2:
			return CSMoveStyle.CROUCH;
		default:
			return null;
		}
	}
	
	public final void setInsIcon(Icon icon) {
		insButton.setIcon(icon);
		insButton.setPreferredSize(new Dimension(icon.getIconWidth(),icon.getIconHeight()));
	}
	
	@Override
	public void attachListener(CompleteListener l) {
		addButton.addActionListener(l);
		delButton.addActionListener(l);
		insButton.addActionListener(l);
	}
	
	@Override
	public void setEnabled(boolean enable) {
		addButton.setEnabled(enable);
		delButton.setEnabled(enable);
		insButton.setEnabled(enable);
		movesBox.setEnabled(enable);
		waitField.setEnabled(enable);
	}

	public int getWaitTime() {
		int ret = -1;
		try {
			ret = Integer.parseInt(waitField.getText());
		}
		catch (NumberFormatException e) {}
		return ret;
	}
	
	public void setIdle() {
		addButton.setIcon(Icons.getIcon("addMove"));
		insButton.setIcon(Icons.getIcon("insMove"));
		delButton.setIcon(Icons.getIcon("delMove"));
	}
	public void setAddingMove() {
		addButton.setIcon(Icons.getIcon("addingMove"));
		delButton.setIcon(Icons.getIcon("delMove"));
		insButton.setIcon(Icons.getIcon("insMove"));
	}
	
	public void setInsertingMove() {
		addButton.setIcon(Icons.getIcon("addMove"));
		delButton.setIcon(Icons.getIcon("delMove"));
		insButton.setIcon(Icons.getIcon("insertingMove"));
	}
	
	public void setDeletingMove() {
		addButton.setIcon(Icons.getIcon("addMove"));
		delButton.setIcon(Icons.getIcon("deletingMove"));
		insButton.setIcon(Icons.getIcon("insMove"));
	}

	public void focusGained(FocusEvent arg0) {
		waitField.selectAll();
	}

	public void focusLost(FocusEvent arg0) {}
}
